<template>
 <div class="admin-login">
        <div class="board">
            <div class="logo">项目管理系统</div>
            <el-form class="form" @submit.native.prevent >
                <el-form-item>
                    <el-input placeholder="账号/手机号/邮箱" clearable="" v-model="form.account"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="密码" type="password" clearable="" v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-switch :inactive-value="0" :active-value="1" v-model="remember"></el-switch>
                    <span class="remark">记住我, 下次自动登录</span>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :style="{width: '100%'}" native-type="submit" @click="login">立即登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
// import lodash from "lodash";
import http from '@/utils/http'
// import qs from 'qs'
export default {
  data () {
   return {
            form: {
                // account: this.$cookie.get("account") || "",
                account:'root',
                password: "123456",
            },
           /*  loginRules:{
                account:[
                    {required:true,message:'请输入账号',trigger:"blur"},
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ],
                password:[
                   {required:true,message:'请输入密码',trigger:"blur"},
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                ]
            }, */
            remember: 1,
        };
  },

  created () {

  },

  methods: {
        clearStorage() {
            window.localStorage.clear();
            window.sessionStorage.clear();
        },
       async login() {
            const { remember } = this;
            const { localStorage, sessionStorage } = window;
            const tokenName = process.env.VUE_APP_TOKEN_KEY;
            // const form = lodash.cloneDeep(this.form);
            // if (remember) this.$cookie.set("account", form.account);
            const {data : res} = await http.post("/user/login", this.form)
            console.log(res)
            if(res.code==200) {
                // 清除之前存储状态
                this.clearStorage();
                if(remember == 1) {
                    localStorage.setItem(tokenName, res.data)
                } else {
                    sessionStorage.setItem(tokenName, res.data)
                }
                this.$router.push({name: 'Console' })
                this.$message({
                    message:res.msg,
                    type: 'success'
                })
            } else {
                this.$message({
                    message: res.msg,
                    type: 'error'
                })
            }
        },
    },
}
</script>

<style lang="scss" scoped>
.admin-login {
    height: 100vh;
    width: 100vw;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #f6f6f6;
    background:url('../../assets/img/bgc.jpg');
    background-size: cover;
    background-position: bottom center;

    .remark {
        font-size: 13px;
        color: gray;
        margin: 0 10px;
    }

    .board {
        width: 360px;
        padding: 20px;
        background-color: #fff;
        border-radius: 10px;
        box-shadow: 0 1px 5px rgba($color: #000000, $alpha: 0.1);

        .logo {
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 40px;
            font-weight: bold;
        }
        .form {
            padding: 30px 0;
        }
    }
}
</style>
